package com.sia.springcloud.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sia.springcloud.mapper.PointMapper;
import com.sia.springcloud.model.Point;
import com.sia.springcloud.protocol.model.OpcuaPoint;
import com.sia.springcloud.service.IPointService;
import com.sia.springcloud.result.GridDataResult;
import com.sia.springcloud.result.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Sirius
 * @Date: 2021/12/21 1:20
 * @Description: 通信协议点位服务层接口实现类
 * 主要提供的接口：添加点位、分页查询点位、批量删除点位、设置点位数采状态
 */

@Service("PointService")
public class PointService extends ServiceImpl<PointMapper, Point> implements IPointService {

    @Resource
    private PointMapper pointMapper;

    /**
     * 添加点位
     *
     * @param point
     * @return
     */
    public String insertPoint(Point point) {

        //查询数据库中是否有相同设备id，相同点位名的设备，如果有则不能插入
        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", point.getDeviceId());
        pointQueryWrapper.eq("point_name", point.getPointName());
        Integer selectCount = pointMapper.selectCount(pointQueryWrapper);
        if (selectCount > 0) {
            return ResultUtil.createSimpleFailureJson("该变量名称已存在");
        }

        pointMapper.insert(point);

        return ResultUtil.createSimpleSuccessJson("变量添加成功");
    }

    /**
     * 根据设备分页查询点位
     *
     * @param start
     * @param limit
     * @param deviceId
     * @return
     */
    public String queryPoint(Integer start, Integer limit, String deviceId) {


        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", deviceId);
        Integer total = pointMapper.selectCount(pointQueryWrapper);

        Page<Point> pointPage = new Page<>();
        pointPage.setTotal(total);
        pointPage.setCurrent(start);
        pointPage.setSize(limit);


        IPage<Point> pointIPage = pointMapper.selectPage(pointPage, pointQueryWrapper);
        List<Point> pointList = pointIPage.getRecords();

        ArrayList<OpcuaPoint> opcuaPointList = new ArrayList<>();
        pointList.forEach(point -> {
            opcuaPointList.add(OpcuaPoint.reconvert(point));
        });


        GridDataResult<OpcuaPoint> gridDataResult = new GridDataResult<>();
        gridDataResult.setTotal(total);
        gridDataResult.setRoot(opcuaPointList);

        return JSON.toJSONString(gridDataResult);
    }


    /**
     * 删除点位
     *
     * @param pointIdList
     * @return
     */
    public String deletePoint(String[] pointIdList) {
        for (int i = 0; i < pointIdList.length; i++) {
            pointMapper.deleteById(Integer.parseInt(pointIdList[i]));
        }
        return ResultUtil.createSimpleSuccessJson("点位信息删除成功");
    }

    /**
     * 修改点位
     *
     * @param point
     * @return
     */
    public String updatePoint(Point point) {

        QueryWrapper<Point> pointQueryWrapper = new QueryWrapper<>();
        pointQueryWrapper.eq("device_id", point.getDeviceId());
        pointQueryWrapper.eq("point_name", point.getPointName());
        pointQueryWrapper.ne("id",point.getId());

        Integer selectCount = pointMapper.selectCount(pointQueryWrapper);
        if (selectCount > 0) {
            return ResultUtil.createSimpleFailureJson("该变量名称已存在");
        }
        pointMapper.updateById(point);
        return ResultUtil.createSimpleSuccessJson("变量修改成功");
    }


    /**
     * 获取点位表格格式
     *
     * @return
     */
    @Override
    public String tableStructure() {
        return null;
    }

}
